package main

import (
	"database/sql"
	_ "github.com/go-sql-driver/mysql"
	"log"
)

type Dinfo struct {
	Username string
	Money int
	Addr string
}

var info_db , _ = sql.Open("mysql", "mt11235:hwqmingdi@tcp(127.0.0.1:3306)/charity?charset=utf8")

func AddDonateInfo(id string, name string, money string, addr string) error {
	_, err := info_db.Exec("insert into donate_info (id, username, money, addr) values (?,?,?,?)", id, name, money, addr)
	return err
}

func AskDonateInfo(id string) (error, []Dinfo) {
	var info = make([]Dinfo, 0)
	rows, err := info_db.Query("select username, money, addr from donate_info where id=?", id)
	defer func() {
		if rows != nil {
			rows.Close()
		}
	}()
	if err != nil {
		return err, nil
	}
	for rows.Next() {
		tmp := new(Dinfo)
		err = rows.Scan(&tmp.Username, &tmp.Money, &tmp.Addr)
		if err != nil {
			log.Println(err)
			return err, nil
		}
		info = append(info, *tmp)
	}
	return nil, info
}